package com.supercube.system.controller;

import com.supercube.common.annotation.Log;
import com.supercube.common.config.Constant;
import com.supercube.common.controller.BaseController;
import com.supercube.common.domain.Tree;
import com.supercube.common.utils.R;
import com.supercube.sport.yoga.utils.ResultInfo;
import com.supercube.system.domain.CubeMenuDO;
import com.supercube.system.domain.MenuDO;
import com.supercube.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author supercube 670328140@qq.com
 */
@RequestMapping("/sys/menu")
@Controller
public class MenuController extends BaseController {
    String prefix = "system/menu";
    @Autowired
    MenuService menuService;

    ////@RequiresPermissions("sys:menu:menu")
    @GetMapping()
    String menu(Model model) {
        return prefix + "/menu";
    }

    //@RequiresPermissions("sys:menu:menu")
    @PostMapping("/menusAllList")
    @ResponseBody
    ResultInfo menusAllList(@RequestBody Map<String, Object> params) {
        //查询一级
        String hideInMenu = String.valueOf(params.get("hideInMenu"));
        if(!Objects.equals("null",hideInMenu)){
            params.put("hideInMenu",hideInMenu);
        }
        params.put("type","0");
        params.put("local","0");
        params.put("parentId","0");
        List<MenuDO> menus = menuService.list(params);
        for(MenuDO d: menus){
            // 查询下级
            Map<String, Object> parm = new HashMap<>();
            if(!Objects.equals("null",hideInMenu)){
                parm.put("hideInMenu",hideInMenu);
            }
            parm.put("type","1");
            parm.put("local","2");
            parm.put("parentId",d.getMenuId().toString());
            List<MenuDO> children = menuService.list(parm);
            d.setChildren(children);
            for(MenuDO c : children){
                Map<String, Object> parmButton = new HashMap<>();
                parmButton.put("type","2");
                parmButton.put("local","2");
                parmButton.put("parentId",c.getMenuId().toString());
                List<MenuDO> buttons = menuService.list(parmButton);
                c.setChildren(buttons);
            }
        }
        return ResultInfo.success(menus);
    }

    //@RequiresPermissions("sys:menu:menu")
    @RequestMapping("/menusList")
    @ResponseBody
    ResultInfo menusList(@RequestParam Map<String, Object> params) {
        //查询一级
        String hideInMenu = String.valueOf(params.get("hideInMenu"));
        if(!Objects.equals("null",hideInMenu)){
            params.put("hideInMenu",hideInMenu);
        }
        params.put("type","0");
        params.put("parentId","0");
        List<MenuDO> menus = menuService.list(params);
        for(MenuDO d: menus){
            // 查询下级
            Map<String, Object> parm = new HashMap<>();
            if(!Objects.equals("null",hideInMenu)){
                parm.put("hideInMenu",hideInMenu);
            }
            parm.put("type","1");
            parm.put("parentId",d.getMenuId().toString());
            List<MenuDO> children = menuService.list(parm);
            d.setChildren(children);
        }
        return ResultInfo.success(menus);
    }

  //@RequiresPermissions("sys:menu:menu")
  @RequestMapping("/cubeMenu")
  @ResponseBody
  ResultInfo cubeMenu(@RequestParam Map<String, Object> params) {
    //查询一级
    String hideInMenu = String.valueOf(params.get("hideInMenu"));
    if(!Objects.equals("null",hideInMenu)){
      params.put("hideInMenu",hideInMenu);
    }
    params.put("type","0");
    params.put("parentId","0");
    List<CubeMenuDO> menus = menuService.cubeMenu(params);
    for(CubeMenuDO d: menus){
      // 查询下级
      Map<String, Object> parm = new HashMap<>();
      if(!Objects.equals("null",hideInMenu)){
        parm.put("hideInMenu",hideInMenu);
      }
      parm.put("type","1");
      parm.put("parentId",d.getMenuId().toString());
      List<CubeMenuDO> children = menuService.cubeMenu(parm);
      d.setChildren(children);
    }
    return ResultInfo.success(menus);
  }

    //@RequiresPermissions("sys:menu:menu")
    @RequestMapping("/menusButton")
    @ResponseBody
    ResultInfo menusButton(@RequestParam Map<String, Object> params) {
        //查询一级
        params.put("type","2");
        List<MenuDO> menus = menuService.list(params);
        return ResultInfo.success(menus);
    }

    //@RequiresPermissions("sys:menu:menu")
    @RequestMapping("/list")
    @ResponseBody
    List<MenuDO> list(@RequestParam Map<String, Object> params) {
        List<MenuDO> menus = menuService.list(params);
        return menus;
    }

    @Log("添加菜单")
    //@RequiresPermissions("sys:menu:add")
    @GetMapping("/add/{pId}")
    String add(Model model, @PathVariable("pId") Long pId) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", menuService.get(pId).getName());
        }
        return prefix + "/add";
    }

    @Log("编辑菜单")
    //@RequiresPermissions("sys:menu:edit")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        MenuDO mdo = menuService.get(id);
        Long pId = mdo.getParentId();
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", menuService.get(pId).getName());
        }
        model.addAttribute("menu", mdo);
        return prefix + "/edit";
    }

    @Log("保存菜单")
    //@RequiresPermissions("sys:menu:add")
    @PostMapping("/save")
    @ResponseBody
    R save(@RequestBody MenuDO menu) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        if (menuService.save(menu) > 0) {
            return R.ok();
        } else {
            return R.error(1, "保存失败");
        }
    }

    @Log("更新菜单")
    //@RequiresPermissions("sys:menu:edit")
    @PostMapping("/update")
    @ResponseBody
    R update(@RequestBody MenuDO menu) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        if (menuService.update(menu) > 0) {
            return R.ok();
        } else {
            return R.error(1, "更新失败");
        }
    }

    @Log("删除菜单")
    //@RequiresPermissions("sys:menu:remove")
    @PostMapping("/remove")
    @ResponseBody
    R remove(@RequestParam(value = "ids[]") List<Long> ids) {
        if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
            return R.error(1, "演示系统不允许修改,完整体验请部署程序");
        }
        if (menuService.remove(ids) > 0) {
            return R.ok();
        } else {
            return R.error(1, "删除失败");
        }
    }

    @GetMapping("/tree")
    @ResponseBody
    Tree<MenuDO> tree() {
        Tree<MenuDO> tree = menuService.getTree();
        return tree;
    }

    @GetMapping("/tree/{roleId}")
    @ResponseBody
    Tree<MenuDO> tree(@PathVariable("roleId") Long roleId) {
        Tree<MenuDO> tree = menuService.getTree(roleId);
        return tree;
    }
}
